# encoding = utf-8
import pandas as pd

from application.logging import logger
from application.utils.MySQLUtils import MySQLUtils


def func_read_station_data(station_id, data_type=None):
    """
    查询热力站历时工况数据: 未处理的(瞬热: status_instant!=2)(累热: status_accum!=2)
    :param station_id: 热力站ID
    :param data_type: instant-瞬热, accum-累热
    :return: 历史工况
    """
    # 查询语句
    sql = ""

    #
    # 转换类型
    station_id = str(station_id)

    #
    # 查询语句:热力站工况数据:瞬热:status_instant(0-未处理，1-正在处理，2-已处理)
    if data_type == "instant":
        sql = f"SELECT * FROM sn_control_model_station WHERE station_id= '{station_id}' AND status_instant!=2"
        pass
    # 查询语句:热力站工况数据:累热:status_accum(0-未处理，1-正在处理，2-已处理)
    elif data_type == "accum":
        sql = f"SELECT * FROM sn_control_model_station WHERE station_id= '{station_id}' AND status_accum!=2"
        pass
    # 数据类型不匹配
    else:
        raise Exception(f"查询历史工况数据异常:data_type只能是[instant 或 accum]")
        pass

    #
    logger.info(f"查询语句: {sql}")

    #
    # try: ->
    try:
        #
        # 获取连接
        con = MySQLUtils.get_mysql_connection()
        #
        # 查询数据
        df = pd.read_sql_query(sql=sql, con=con)
        #
        logger.info(f"查询结果[历史工况数据]: \n {df}")
        #
        # 筛选数据
        df = df[
            [
                "station_id",  # 热力站ID
                "collect_time",  # 采集时间
                "second_supply_water_temp",  # 二次供水温度
                "second_back_water_temp",  # 二次回水温度
                "total_instant_heat",  # 瞬时热量
                "total_accum_heat"  # 累计热量
            ]
        ]
        #
        # 打印数据
        logger.info(f"筛选结果[历史工况数据]: \n {df}")
        logger.info(f"筛选结果[历史工况数据][瞬热,累热]: \n {df[['total_instant_heat', 'total_accum_heat']]}")
        return df
        pass  # <- try:
    except BaseException as err:  # except ->
        logger.error(f"查询历史工况数据出错: {err}")
        pass  # <- except

    pass  # <- connect_mysql_read_station_data


pass

if __name__ == '__main__':
    # connect_mysql_read_station_data(station_id="1353374478217908224", data_type="instant")
    func_read_station_data(station_id="1353374478217908224", data_type="accum")
    pass
